import { BaseWindow } from "../../../../component/window/BaseWindow";
import { GTextInput } from "../../../../../com/mui/controls/GTextInput";
import { Language } from "../../../../../extend/language/Language";
import { FriendStyle } from "../FriendStyle";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { ResouceConst } from "../../../../component/gconst/ResouceConst";
import { Global } from "../../../../../com/gengine/global/Global";
import { KeyCode } from "../../../../../com/gengine/keyBoard/KeyCode";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { PlayerMiniInfo } from "../../../model/PlayerMiniInfo";
import { PlayerOpMenuConst } from "../../common/menu/PlayerOpMenuConst";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { ObjCreate } from "../../common/ObjCreate";
import { PlayerOpRegister } from "../../common/menu/PlayerOpRegister";
import { Cache } from "../../../cache/Cache";
import { ScaleBitmap } from "../../../../../com/mui/display/ScaleBitmap";
type int = number;
//class FriendsSearchPlayer
    
    export  class FriendsSearchPlayer extends BaseWindow
    {
        constructor(arg1: string="")
        {
            super();
            this._textInputRoleName = new GTextInput();
            this._searchText = arg1;
            this._this = this;
            this.init();
            _windowCenter.y = 33;
            _titleHeight = 26;
            return;
        }

        /* internal  */init(): void
        {
            setSize(304, 228);
            this.titleHeight = 33;
            title = Language.getString(40321);
            this.create();
            this.addListeners();
            return;
        }

        /* internal  */create(): void
        {
            this._textRoleName = new Object /* flash.text.TextField */();
            this._textRoleName.defaultTextFormat = FriendStyle.textFormat1;
            this._textRoleName.text = Language.getString(40322);
            this._textRoleName.x = 35;
            this._textRoleName.y = 42;
            this._textRoleName.height = 20;
            this._textRoleName.width = 54;
            this._textRoleName.filters = [FilterConst.glowFilter];
            this.addChild(this._textRoleName);
            this._textInputRoleName = new GTextInput();
            this._textInputRoleName.styleName = "GTextInput";
            this._textInputRoleName.x = 89;
            this._textInputRoleName.y = 43;
            this._textInputRoleName.width = 115;
            this._textInputRoleName.height = 20;
            this._textInputRoleName.text = this._searchText;
            this.addChild(this._textInputRoleName);
            this._btnSearch = new GButton();
            this._btnSearch.x = 213;
            this._btnSearch.y = 43;
            this._btnSearch.width = 47;
            this._btnSearch.height = 22;
            this._btnSearch.styleName = "Button";
            this._btnSearch.textField.filters = [FilterConst.glowFilter];
            this._btnSearch.label = Language.getString(40323);
            this.addChild(this._btnSearch);
            this._textInputRoleName.setStyle("textFormat", FriendStyle.textFormat1);
            this._bgMessage = ResouceConst.getScaleBitmap("WindowCenterB");
            this._bgMessage.x = 17;
            this._bgMessage.y = 71;
            this._bgMessage.width = 269;
            this._bgMessage.height = 115;
            this.addChild(this._bgMessage);
            this._textTips = new Object /* flash.text.TextField */();
            this._textTips.x = 22;
            this._textTips.y = 80;
            this._textTips.width = 200;
            this._textTips.height = 20;
            this._textTips.defaultTextFormat = FriendStyle.textFormat2;
            this._textTips.text = Language.getString(40320);
            this._textTips.visible = false;
            this.addChild(this._textTips);
            this._playerMessagePanel = new Object /* flash.display.Sprite */();
            this._playerMessagePanel.x = 28;
            this._playerMessagePanel.y = 82;
            this._playerMessagePanel.visible = false;
            this.addChild(this._playerMessagePanel);
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show();
            if (this._textInputRoleName && Global.stage.contains(this._textInputRoleName)) 
            {
                this._textInputRoleName.setFocus();
                this._textInputRoleName.setSelection(this._textInputRoleName.text.length, this._textInputRoleName.text.length);
            }
            return;
        }

        /* internal  */addListeners(): void
        {
            this._btnSearch.addEventListener(flash.events.MouseEvent.CLICK, this.searchHandler);
            this._textInputRoleName.addEventListener(flash.events.KeyboardEvent.KEY_DOWN, this.keyHandler);
            return;
        }

        /* internal  */searchHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.search(this._textInputRoleName.text);
            return;
        }

        /* internal  */keyHandler(arg1: Object /* flash.events.KeyboardEvent */): void
        {
            if (arg1.keyCode == KeyCode.ENTER) 
            {
                this.search(this._textInputRoleName.text);
            }
            return;
        }

        public search(arg1: string): void
        {
            if (arg1 != "") 
            {
                this._textInputRoleName.text = arg1;
                Dispatcher.dispatchEvent(new DataEvent(EventName.FriendSearchPlayer, arg1));
            }
            return;
        }

        public showSearchResults(arg1: Boolean, arg2: PlayerMiniInfo): void
        {
            if (arg1) 
            {
                this._textTips.visible = false;
                this._playerMessagePanel.visible = true;
                this.createPlayerMessage(arg2);
            }
            else 
            {
                this._textTips.visible = true;
                this._playerMessagePanel.visible = false;
            }
            return;
        }

        public createPlayerMessage(arg1: PlayerMiniInfo): void
        {
            var objMiniPlayer: PlayerMiniInfo;
            var i: int;
            var dataArray: Array<any>;
            var btnAddFriend: GButton;
            var btnAddToBlack: GButton;
            var AddFriendHandler: Function;
            var AddToBlackHandler: Function;
            var ChatPrivateHandler: Function;
            var textField: Object /* flash.text.TextField */;
            var btn: GButton;
            var btnChatPrivate: GButton;

            var loc1;
            i = 0;
            AddFriendHandler = null;
            AddToBlackHandler = null;
            ChatPrivateHandler = null;
            textField = null;
            btn = null;
            btnChatPrivate = null;
            objMiniPlayer = arg1;
            AddFriendHandler = (arg1: Object /* flash.events.MouseEvent */): void
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.FriendRequestAddFriend, {"name": objMiniPlayer.playerName}));
                _this.hide();
                return;
            }
            AddToBlackHandler = (arg1: Object /* flash.events.MouseEvent */): void
            {
                PlayerOpMenuConst.Opearte(PlayerOpMenuConst.MoveToBlackList, objMiniPlayer);
                _this.hide();
                return;
            }
            ChatPrivateHandler = (arg1: Object /* flash.events.MouseEvent */): void
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ChatPrivate, objMiniPlayer));
                return;
            }
            i = (this._playerMessagePanel.numChildren - 1);
            while (i >= 0) 
            {
                this._playerMessagePanel.removeChildAt(i);
                --i;
            }
            dataArray = [{"name": Language.getString(40324), "value":""}, {"name": Language.getString(40325), "value": objMiniPlayer.level}, {"name": Language.getString(40326), "value": objMiniPlayer.campName}, {"name": Language.getString(40327), "value": objMiniPlayer.career}, {"name": Language.getString(40328), "value": objMiniPlayer.sexName}, {"name": Language.getString(40329), "value": objMiniPlayer.online ? Language.getString(40330) : Language.getString(40331)}];
            i = 0;
            while (i < dataArray.length) 
            {
                textField = new Object /* flash.text.TextField */();
                textField.x = i % 2 * 137;
                textField.y = Math.floor(i / 2) * 30;
                textField.width = 130;
                textField.height = 20;
                textField.htmlText = HTMLUtil.addColor((dataArray[i] as Object).name, FriendStyle.color1) + HTMLUtil.addColor((dataArray[i] as Object).value, objMiniPlayer.online ? FriendStyle.color2 : GlobalStyle.color3);
                this._playerMessagePanel.addChild(textField);
                if (i == 0) 
                {
                    btn = ObjCreate.createGButton(objMiniPlayer.playerName, 44, -5, 100, 28, this._playerMessagePanel, "LinkButton");
                    btn.x = btn.textField.textWidth / 2;
                    PlayerOpRegister.Register(btn, objMiniPlayer);
                }
                ++i;
            }
            btnAddFriend = new GButton();
            btnAddFriend.x = 108;
            btnAddFriend.y = 108;
            btnAddFriend.width = 70;
            btnAddFriend.height = 26;
            btnAddFriend.styleName = "Button";
            btnAddFriend.label = Language.getString(40332);
            btnAddFriend.enabled = objMiniPlayer.online && !Cache.instance.friend.isFriend(objMiniPlayer.playerName);
            this._playerMessagePanel.addChild(btnAddFriend);
            btnAddToBlack = new GButton();
            btnAddToBlack.x = 185;
            btnAddToBlack.y = 108;
            btnAddToBlack.width = 70;
            btnAddToBlack.height = 26;
            btnAddToBlack.label = Language.getString(40333);
            btnAddToBlack.styleName = "Button";
            btnAddToBlack.enabled = !Cache.instance.friend.isInBlackList(objMiniPlayer.playerName);
            this._playerMessagePanel.addChild(btnAddToBlack);
            if (Cache.instance.friend.isFriendByPlayerId(objMiniPlayer.playerId)) 
            {
                btnChatPrivate = new GButton();
                btnChatPrivate.x = 31;
                btnChatPrivate.y = 108;
                btnChatPrivate.width = 70;
                btnChatPrivate.height = 26;
                btnChatPrivate.label = Language.getString(40410);
                btnChatPrivate.styleName = "Button";
                btnChatPrivate.addEventListener(flash.events.MouseEvent.CLICK, ChatPrivateHandler, false, 0, true);
                this._playerMessagePanel.addChild(btnChatPrivate);
            }
            btnAddFriend.addEventListener(flash.events.MouseEvent.CLICK, AddFriendHandler, false, 0, true);
            btnAddToBlack.addEventListener(flash.events.MouseEvent.CLICK, AddToBlackHandler, false, 0, true);
            return;
        }

        /* internal */ /* var */_searchText: string;

        /* internal */ /* var */_textRoleName: Object /* flash.text.TextField */;

        /* internal */ /* var */_textInputRoleName: GTextInput;

        /* internal */ /* var */_btnSearch: GButton;

        /* internal */ /* var */_bgMessage: ScaleBitmap;

        /* internal */ /* var */_textTips: Object /* flash.text.TextField */;

        /* internal */ /* var */_playerMessagePanel: Object /* flash.display.Sprite */;

        /* internal */ /* var */_this: FriendsSearchPlayer;
    }
